<?php

namespace tmcore\services\notice\wechat;

use EasyWeChat\Kernel\HttpClient\Response;
use EasyWeChat\OfficialAccount\Application;
use Psr\Http\Message\ResponseInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use tmcore\exceptions\WeChatException;
use tmcore\services\wechat\WxConfigService;

class WechatTemplateService
{
    protected Application $app;

    protected array $config;

    /**
     * 初始化
     */
    public function __construct()
    {
        $this->config = $this->getConfig();
        $this->app = new Application($this->config);
    }

    /**
     * 获取配置
     * @return array
     * @throws WeChatException
     */
    protected function getConfig()
    {
        $config = WxConfigService::getOaConfig();
        if (empty($config['app_id']) || empty($config['secret'])) {
            throw new WeChatException('请先设置微信公众号配置');
        }
        return $config;
    }


    /**
     * 获取支持的行业列表
     * @return Response|\Symfony\Contracts\HttpClient\ResponseInterface
     * @throws TransportExceptionInterface
     */
    public function getIndustry() {
        return $this->app->getClient()->get('cgi-bin/template/get_industry');
    }

    /**
     * 添加模板
     * @param $shortId
     * @param $keyword_name_list
     * @return array|Collection|object|ResponseInterface|string
     * @throws TransportExceptionInterface
     */
    public function addTemplate($shortId, $keyword_name_list) {
        return $this->app->getClient()->postJson('cgi-bin/template/api_add_template', [
            'template_id_short' => $shortId,
            'keyword_name_list' => $keyword_name_list
        ]);
    }


    /**
     * 删除模板
     * @param $templateId
     * @return Response|\Symfony\Contracts\HttpClient\ResponseInterface
     * @throws TransportExceptionInterface
     */
    public function deleteTemplate($templateId) {
        return $this->app->getClient()->postJson('cgi-bin/template/del_private_template', [
            'template_id' => $templateId,
        ]);
    }


    /**
     * 获取模板列表
     * @return Response|\Symfony\Contracts\HttpClient\ResponseInterface
     * @throws TransportExceptionInterface
     */
    public function getTemplateList() {
        return $this->app->getClient()->get('cgi-bin/template/get_all_private_template');
    }


    /**
     * 发送模板消息
     * @param $params
     * @return Response|\Symfony\Contracts\HttpClient\ResponseInterface
     * @throws TransportExceptionInterface
     */
    public function sendTemplate($params) {
        return $this->app->getClient()->postJson('cgi-bin/message/template/send', [
                'touser' => $params['openid'],
                'template_id' => $params['template_id'],
                'url' => $params['url']??'',
                'miniprogram' => [
                    'appid' => $params['mnp_appid']??'',
                    'pagepath' => $params['mnp_page']??'',
                ],
                'data' => $params['data'],
            ]
        );
    }


    /**
     * 发送一次性订阅消息
     * @param $params
     * @return Response|\Symfony\Contracts\HttpClient\ResponseInterface
     * @throws TransportExceptionInterface
     */
    public function sendSubscription($params) {
        return $this->app->getClient()->postJson('cgi-bin/message/template/send', [
            'touser' => $params['openid'],
            'template_id' => $params['templateId'],
            'url' => $params['url']??'',
            'scene' => 1000,
            'data' => $params['data'],
        ]);
    }
}